﻿<template>
	<view class="shzy-wrap shzy-wrap-form">
		<u-form :model="dataForm" :rules="rules" ref="dataForm" :errorType="['toast']" label-position="right" label-width="100" label-align="left" class="shzy-form">
			<u-form-item label="单据类型" prop="type" required >
                <shzy-select v-model="dataForm.type" :options="typeOptions" :props="typeProps" placeholder='请选择' ></shzy-select>
            </u-form-item>
			<u-form-item label="状态" prop="status" required >
				<u-radio-group v-model="dataForm.status" >
					<u-radio v-for="(option, index) in statusOptions" :key="index" :name="option['id']">{{option['fullName']}}</u-radio>
                </u-radio-group>
            </u-form-item>
			<u-form-item label="时间单位" prop="timeUnit" required >
				<u-radio-group v-model="dataForm.timeUnit" >
					<u-radio v-for="(option, index) in timeUnitOptions" :key="index" :name="option['id']">{{option['fullName']}}</u-radio>
                </u-radio-group>
            </u-form-item>
			<u-form-item label="时间最小值" prop="minValue" required >
                <u-number-box v-model="dataForm.minValue" :input-width="120" :input-height="60" :min='0' :step='1' ></u-number-box>
            </u-form-item>
			<u-form-item label="时间最大值" prop="maxValue" required >
                <u-number-box v-model="dataForm.maxValue" :input-width="120" :input-height="60" :min='1' :step='1' ></u-number-box>
            </u-form-item>
			<u-form-item label="消息接收人" prop="recipientList" required >
                <shzy-user-select  v-model="dataForm.recipientList" multiple placeholder='请选择' ></shzy-user-select>
            </u-form-item>
			<u-form-item label="消息模板" prop="messageTemplate" required >
                <shzy-select v-model="dataForm.messageTemplate" :options="messageTemplateOptions" :props="messageTemplateProps" placeholder='请选择' ></shzy-select>
            </u-form-item>
			<u-form-item label="有效标志" prop="enabledMark" required >
                <view class="u-flex u-form-item-switch">
					<shzy-switch v-model="dataForm.enabledMark" ></shzy-switch>
                </view>
            </u-form-item>
		</u-form>
		<view class="buttom-actions" v-if="jurisdictionType != 'btn_detail'">
			<u-button class="buttom-btn" @click="shzy.goBack()">取 消</u-button>
			<u-button class="buttom-btn" type="primary" @click="submitForm" :loading="btnLoading">确 定</u-button>
		</view>
	</view>
</template>
<script>
    import { getDictionaryDataSelector,getDataInterfaceRes } from '@/api/common'
    import request from '@/utils/request'
    export default {
        data() {
            return {
                btnLoading: false,
                dataForm: {
                    id:'',
					type:undefined,
					status:"1",
					timeUnit:"day",
					minValue:0,
					maxValue:1,
					recipientList:[],
					messageTemplate:undefined,
					enabledMark:1,
                },
                rules: {
					type:[
						{
							required:true,
							message:'请输入单据类型',
							trigger:'"change"'
						},
					],
					status:[
						{
							required:true,
							message:'请输入状态',
							trigger:'"change"'
						},
					],
					timeUnit:[
						{
							required:true,
							message:'请输入时间单位',
							trigger:'"change"'
						},
					],
					minValue:[
						{
							required:true,
							message:'请输入时间最小值',
							trigger:["blur","change"]
						},
					],
					maxValue:[
						{
							required:true,
							message:'请输入时间最大值',
							trigger:["blur","change"]
						},
					],
					recipientList:[
						{
							required:true,
							message:'请输入消息接收人',
							trigger:'"click"'
						},
					],
					messageTemplate:[
						{
							required:true,
							message:'请输入消息模板',
							trigger:'"change"'
						},
					],
					enabledMark:[
						{
							required:true,
							message:'请输入有效标志',
							trigger:'change'
						},
					],
                },
				jurisdictionType:'',
				typeProps:{'label':'fullName','value':'id'},
				typeOptions : [],
				statusProps:{'label':'fullName','value':'id'},
				statusOptions:[{'fullName':"即将逾期",'id':"1"},{'fullName':"逾期",'id':"2"}],
				timeUnitProps:{'label':'fullName','value':'id'},
				timeUnitOptions:[{'fullName':"天",'id':"day"},{'fullName':"小时",'id':"hour"},{'fullName':"分钟",'id':"minute"},{'fullName':"秒",'id':"second"}],
				messageTemplateProps:{'label':'fullName','value':'id'},
				messageTemplateOptions:[],
            };
        },
		watch: {
        },
        onLoad(option) {
			this.jurisdictionType = option.jurisdictionType
			let _title = ''
			if(option.jurisdictionType =='btn_add'){
				_title = "新增"
            }
			if(option.jurisdictionType =='btn_edit'){
				_title = "编辑"
            }
			if(option.jurisdictionType =='btn_detail'){
				_title = ""
            }
            this.dataForm.id = option.id || ''
            uni.setNavigationBarTitle({
                title: _title
            })
            this.initData()
			this.gettypeOptions();
			this.selfInit();
        },
        onReady() {
            this.$refs.dataForm.setRules(this.rules);
        },
        methods: {
			gettypeOptions(){
				getDictionaryDataSelector('459581510966602757').then(res => {
					this.typeOptions = res.data.list
				});
			},
			selfInit(){
			},
			resetForm() {
				this.$refs.dataForm.resetFields();
				this.selfInit()
			},
            initData() {
				this.$store.commit('base/UPDATE_RELATION_DATA', {})
                if (this.dataForm.id) {
                    request({
                        url: '/api/system/QmsBaseOverduesetting/' + this.dataForm.id,
                        method: 'get',
                    }).then(res => {
                        this.dataForm = res.data;
                    })
                }
            },
			submitForm() {
                this.$refs.dataForm.validate(valid => {
                    if (!valid) return
                    this.btnLoading = true
                    if (this.dataForm.id) {
                        request({
                            url: '/api/system/QmsBaseOverduesetting/' + this.dataForm.id,
                            method: 'put',
                            data: this.dataForm,
                        }).then(res => {
                            uni.showToast({
                                title: res.msg,
                                complete: () => {
                                    setTimeout(() => {
                                        this.btnLoading = false
                                        uni.$emit('refresh')
                                        uni.navigateBack()
                                    }, 1500)
                                }
                            })
                        })
                    } else {
                        request({
                            url: '/api/system/QmsBaseOverduesetting',
                            method: 'post',
                            data: this.dataForm,
                        }).then(res => {
                            uni.showToast({
                                title: res.msg,
                                complete: () => {
                                    setTimeout(() => {
                                        this.btnLoading = false
                                        uni.$emit('refresh')
                                        uni.navigateBack()
                                    }, 1500)
                                }
                            })
                        })
                    }
                });
            },
        }
    };
</script>

